package com.example.demo;

import org.springframework.util.DigestUtils;

public class LbsTest {

    private static final String VALUE_STRING="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    //国内wifi
//    static String data = "{\"region\":1,\"type\":\"wifi\",\"wifi\":{\"wifiAccessPoints\":[{\"ssid\":\"tplink\",\"macAddress\":\"EC:6C:9F:4B:E8:89\",\"signalStrength\":-94},{\"macAddress\":\"EC:6C:9F:4B:E8:7A\",\"signalStrength\":-100,\"ssid\":\"neoway\"}]}}";
//    static String formatData = "{\\\"region\\\":1,\\\"type\\\":\\\"wifi\\\",\\\"wifi\\\":{\\\"wifiAccessPoints\\\":[{\\\"ssid\\\":\\\"tplink\\\",\\\"macAddress\\\":\\\"EC:6C:9F:4B:E8:89\\\",\\\"signalStrength\\\":-94},{\\\"macAddress\\\":\\\"EC:6C:9F:4B:E8:7A\\\",\\\"signalStrength\\\":-100,\\\"ssid\\\":\\\"neoway\\\"}]}}";

    //国外WiFi
//    static String data = "{\"region\":2,\"type\":\"wifi\",\"wifi\":{\"wifiAccessPoints\": [{\"macAddress\": \"04:bd:88:4d:66:e0\",\"signalStrength\": -43,\"signalToNoiseRatio\": 0},{\"macAddress\": \"44:48:c1:a6:f3:d0\",\"signalStrength\": -55,\"signalToNoiseRatio\": 0}]}}";
//    static String formatData = "{\\\"region\\\":2,\\\"type\\\":\\\"wifi\\\",\\\"wifi\\\":{\\\"wifiAccessPoints\\\": [{\\\"macAddress\\\": \\\"04:bd:88:4d:66:e0\\\",\\\"signalStrength\\\": -43,\\\"signalToNoiseRatio\\\": 0},{\\\"macAddress\\\": \\\"44:48:c1:a6:f3:d0\\\",\\\"signalStrength\\\": -55,\\\"signalToNoiseRatio\\\": 0}]}}";

    //国内基站
    static String data = "{\"region\":1,\"type\":\"cellular\",\"cellular\":{\"homeMobileCountryCode\": 460,\"homeMobileNetworkCode\": 1,\"cellTowers\": [{\"cellId\": \"198073347\",\"locationAreaCode\": \"37223\",\"mobileCountryCode\": \"460\",\"mobileNetworkCode\": \"1\",\"signalStrength\": 0,\"timingAdvance\": 0,\"age\": 0 }]}}";
    static String formatData = "{\\\"region\\\":1,\\\"type\\\":\\\"cellular\\\",\\\"cellular\\\":{\\\"homeMobileCountryCode\\\": 460,\\\"homeMobileNetworkCode\\\": 1,\\\"cellTowers\\\": [{\\\"cellId\\\": \\\"198073347\\\",\\\"locationAreaCode\\\": \\\"37223\\\",\\\"mobileCountryCode\\\": \\\"460\\\",\\\"mobileNetworkCode\\\": \\\"1\\\",\\\"signalStrength\\\": 0,\\\"timingAdvance\\\": 0,\\\"age\\\": 0 }]}}";
//"region":1,
//        \"region\":1,

    //国外基站
//    static String data = "{\"region\":2,\"type\":\"cellular\",\"cellular\":{\"cellTowers\": [{\"cellId\": 170402199,\"locationAreaCode\": 35632,\"mobileCountryCode\": 310,\"mobileNetworkCode\": 410,\"age\": 0,\"signalStrength\": -60,\"timingAdvance\": 15}]}}";
//    static String formatData = "{\\\"region\\\":2,\\\"type\\\":\\\"cellular\\\",\\\"cellular\\\":{\\\"cellTowers\\\": [{\\\"cellId\\\": 170402199,\\\"locationAreaCode\\\": 35632,\\\"mobileCountryCode\\\": 310,\\\"mobileNetworkCode\\\": 410,\\\"age\\\": 0,\\\"signalStrength\\\": -60,\\\"timingAdvance\\\": 15}]}}";
    public static void main(String[] args) {
        String userId = "74016f4f092e4c06b19b286245a13bf2";
        String apiKey = "a09c95189e884da2b01e714b0bf6be62";
        Long timestamp = System.currentTimeMillis() ;


        String nonceStr=generateNonceStr(32);
        System.out.println("data: " + data);
        System.out.println("userId: " + userId);
        System.out.println("apiKey: " + apiKey);
        System.out.println("nonceStr: " + nonceStr);
        System.out.println("timestamp: " + timestamp);

        String param = getSign(data, userId, nonceStr, timestamp, apiKey);
        System.out.println(param);
    }

    private static String getSign(String data, String userId, String nonceStr,Long timestamp, String apiKey){



        String stringA = "data=" + data + '&' +
                "nonceStr=" + nonceStr + '&' +
                "timestamp=" + timestamp + '&' +
                "userId=" + userId
                ;
        //第一步：对参数按照key=value的格式，并按照参数名ASCII字典序排序得到stringA
        //第二步：拼接apikey
        String stringSignTemp = stringA + "&apiKey=" + apiKey;
        System.out.println("stringSignTemp: " + stringSignTemp);
        //第三步：签名
        String localSign = DigestUtils.md5DigestAsHex(stringSignTemp.getBytes()).toUpperCase();
        System.out.println("localSign: " + localSign);

        String param = "{" +
                "\"userId\": \"" + userId + '\"' +',' +
                "\"nonceStr\": \"" + nonceStr + '\"' + ',' +
                "\"timestamp\": \"" + timestamp + '\"' + ',' +
                "\"sign\": \"" + localSign + '\"' + ',' +
                "\"data\": \"" + formatData + '\"' +
                "}"
                ;
        return param;
    }

    private static String generateNonceStr(int length){
        String code="";
        for (int i=0; i<length; i++) {
            code+=VALUE_STRING.charAt((int) (Math.random() *100) %VALUE_STRING.length());
        }
        return code;
    }
}